﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using CRM.MODEL;
using CRM.UTIL;

namespace CRM.DAL
{
    public class PartsBrandsD : DBChannel
    {
        /// <summary>
        /// 可以在此选择数据库连接，默认为CRM
        /// </summary>
        public PartsBrandsD() : base()
        {
        }


        #region 执行存储过程 Pro_GetPartsBrands ,根据条件获取回馈信息

        /// <summary>
        /// 执行存储过程 Pro_GetPartsBrands 
        ///xialei
        /// </summary>

        public DataTable ExecuteGetPartsBrands(ref CRM.MODEL.GetPartsBrands stu)
        {
            //存储过程参数	 		     
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.IDPARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.BRANDSNAMEPARM, SqlDbType.VarChar, 100));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.BRANDSDISPLAYNAMEPARM,
                SqlDbType.VarChar, 100));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.REMARKPARM, SqlDbType.VarChar, 500));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.ISAVAILABLEPARM, SqlDbType.Bit));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.CREATEBYPARM, SqlDbType.VarChar, 20));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.CREATETIMEPARM, SqlDbType.DateTime));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.LASTMODIFYBYPARM, SqlDbType.VarChar, 20));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.LASTMODIFYTIMEPARM, SqlDbType.DateTime));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.KEYVALUEPARM, SqlDbType.VarChar));

            //分页参数
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.ORDERSTRING_PARM, SqlDbType.VarChar,
                1000));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.PAGESIZE_PARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.CURRENTPAGEINDEX_PARM, SqlDbType.Int));
            this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.GetPartsBrands.TOTALITEMS_PARM, SqlDbType.Decimal));

            //设置参数的值
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.IDPARM].Value = stu.IDVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.BRANDSNAMEPARM].Value = stu.BRANDSNAMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.BRANDSDISPLAYNAMEPARM].Value = stu.BRANDSDISPLAYNAMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.REMARKPARM].Value = stu.REMARKVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.ISAVAILABLEPARM].Value = stu.ISAVAILABLEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.CREATEBYPARM].Value = stu.CREATEBYVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.CREATETIMEPARM].Value = stu.CREATETIMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.LASTMODIFYBYPARM].Value = stu.LASTMODIFYBYVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.LASTMODIFYTIMEPARM].Value = stu.LASTMODIFYTIMEVALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.KEYVALUEPARM].Value = stu.KEYVALUEVALUE;

            //设置分页参数的值
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.ORDERSTRING_PARM].Value = stu.ORDERSTRING_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.PAGESIZE_PARM].Value = stu.PAGESIZE_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.CURRENTPAGEINDEX_PARM].Value = stu.CURRENTPAGEINDEX_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.TOTALITEMS_PARM].Value = stu.TOTALITEMS_VALUE;
            this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.TOTALITEMS_PARM].Direction = ParameterDirection.Output;

            //执行
            DataTable dt = this.GetDataByProcedure("dbo.Pro_GetPartsBrands");
            //获取数据库的信息
            stu.TOTALITEMS_VALUE = this.sqlCmd.Parameters[CRM.MODEL.GetPartsBrands.TOTALITEMS_PARM].Value;

            return dt;
        }

        #endregion

        #region 执行存储过程 Pro_EditPartsBrands,根据条件获取回馈信息

        /// <summary>
        /// 执行存储过程 Pro_EditPartsBrands 的相关参数
        ///xialei
        /// </summary>
        public int ExecuteEditPartsBrands(ref CRM.MODEL.EditPartsBrands stu)
        {
            if ((string) stu.ACTION_VALUE == "Update")
            {
                #region Update

                var sqlSb = new StringBuilder();
                sqlCmd.Parameters.Clear();
                sqlSb.Append(" UPDATE PartsBrands SET ");

                if (stu.BRANDSNAME_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" BrandsName=@BrandsName,");
                    sqlCmd.Parameters.Add(new SqlParameter("@BRANDSNAME", stu.BRANDSNAME_VALUE));
                }
                if (stu.BRANDSDISPLAYNAME_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" BrandsDisplayName=@BrandsDisplayName,");
                    sqlCmd.Parameters.Add(new SqlParameter("@BRANDSDISPLAYNAME", stu.BRANDSDISPLAYNAME_VALUE));
                }
                if (stu.REMARK_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" Remark=@Remark,");
                    sqlCmd.Parameters.Add(new SqlParameter("@REMARK", stu.REMARK_VALUE));
                }
                if (stu.ISAVAILABLE_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" IsAvailable=@IsAvailable,");
                    sqlCmd.Parameters.Add(new SqlParameter("@ISAVAILABLE", stu.ISAVAILABLE_VALUE));
                }
                if (stu.CREATEBY_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" CreateBy=@CreateBy,");
                    sqlCmd.Parameters.Add(new SqlParameter("@CREATEBY", stu.CREATEBY_VALUE));
                }
                if (stu.CREATETIME_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" CreateTime=@CreateTime,");
                    sqlCmd.Parameters.Add(new SqlParameter("@CREATETIME", stu.CREATETIME_VALUE));
                }
                if (stu.LASTMODIFYBY_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" LastModifyBy=@LastModifyBy,");
                    sqlCmd.Parameters.Add(new SqlParameter("@LASTMODIFYBY", stu.LASTMODIFYBY_VALUE));
                }
                if (stu.LASTMODIFYTIME_VALUE != DBNull.Value)
                {
                    sqlSb.Append(" LastModifyTime=@LastModifyTime,");
                    sqlCmd.Parameters.Add(new SqlParameter("@LASTMODIFYTIME", stu.LASTMODIFYTIME_VALUE));
                }

                var sql = sqlSb.ToString().TrimEnd(',');

                sqlCmd.Parameters.Add(new SqlParameter("@ID", stu.ID_VALUE));
                //sqlCmd.CommandText = sqlSb.ToString();
                //sqlCmd.ExecuteNonQuery();
                int i = base.ExecuteSQL(sql + " WHERE ID=@ID");

                return i;

                #endregion
            }
            else
            {
                //动作参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.ACTION_PARM, SqlDbType.VarChar, 20));
                //错误信息参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.ERRMSG_PARM, SqlDbType.VarChar,
                    100));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.OUTID_PARM, SqlDbType.Int));
                ////表字段参数
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.IDPARM, SqlDbType.Int));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.BRANDSNAMEPARM, SqlDbType.VarChar,
                    100));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.BRANDSDISPLAYNAMEPARM,
                    SqlDbType.VarChar, 100));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.REMARKPARM, SqlDbType.VarChar, 500));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.ISAVAILABLEPARM, SqlDbType.Bit));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.CREATEBYPARM, SqlDbType.VarChar,
                    20));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.CREATETIMEPARM, SqlDbType.DateTime));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.LASTMODIFYBYPARM,
                    SqlDbType.VarChar, 20));
                this.sqlCmd.Parameters.Add(new SqlParameter(CRM.MODEL.EditPartsBrands.LASTMODIFYTIMEPARM,
                    SqlDbType.DateTime));



                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.ACTION_PARM].Value = stu.ACTION_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.ERRMSG_PARM].Direction = ParameterDirection.Output;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.OUTID_PARM].Direction = ParameterDirection.Output;
                //设置参数的值
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.IDPARM].Value = stu.ID_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.BRANDSNAMEPARM].Value = stu.BRANDSNAME_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.BRANDSDISPLAYNAMEPARM].Value =
                    stu.BRANDSDISPLAYNAME_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.REMARKPARM].Value = stu.REMARK_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.ISAVAILABLEPARM].Value = stu.ISAVAILABLE_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.CREATEBYPARM].Value = stu.CREATEBY_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.CREATETIMEPARM].Value = stu.CREATETIME_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.LASTMODIFYBYPARM].Value = stu.LASTMODIFYBY_VALUE;
                this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.LASTMODIFYTIMEPARM].Value = stu.LASTMODIFYTIME_VALUE;

                int i = this.ExecuteProcedure("dbo.Pro_EditPartsBrands"); //执行	
                //获取数据库的信息
                stu.ERRMSG_VALUE = this.sqlCmd.Parameters[CRM.MODEL.EditPartsBrands.ERRMSG_PARM].Value;
                return i;
            }
        }

        #endregion
    }
}